package com.rslai.commons.validator.jsonvalidator.validators;

import com.rslai.commons.validator.jsonvalidator.exception.ValidatorArgumentException;
import com.rslai.commons.validator.jsonvalidator.matchers.JsonMatchers;
import org.junit.Assert;

import java.math.BigDecimal;
import java.util.List;

/**
 * JSON校验器，最小值校验器
 */
public class MinValidator extends NumericValidator {
    public static final String VALIDATOR_NAME = "min";

    private static final String MESSAGE = "%s 的最小值是 %s, 但实际是 %s";
    private Number number;

    /**
     * 构造函数
     * @param arguments arguments
     */
    public MinValidator(List<Object> arguments) {
        if (arguments == null || !(arguments.get(0) instanceof Number)) {
            throw new ValidatorArgumentException("最小值校验器参数异常。");
        }
        number = new BigDecimal(arguments.get(0).toString());
    }

    /**
     * Long校验
     * @param longValue longValue
     */
    @Override
    protected void validateLong(long longValue) {
        String reason = String.format(MESSAGE, getCurrentKeyPath(), number, longValue);
        Assert.assertThat(reason, longValue, JsonMatchers.min(number));
    }

    /**
     * Integer校验
     * @param intValue intValue
     */
    @Override
    protected void validateInteger(int intValue) {
        String reason = String.format(MESSAGE, getCurrentKeyPath(), number, intValue);
        Assert.assertThat(reason, intValue, JsonMatchers.min(number));
    }

    /**
     * Integer校验
     * @param bigDecimalValue bigDecimalValue
     */
    @Override
    protected void validateBigDecimal(BigDecimal bigDecimalValue) {
        String reason = String.format(MESSAGE, getCurrentKeyPath(), number, bigDecimalValue);
        Assert.assertThat(reason, bigDecimalValue, JsonMatchers.min(number));
    }

    /**
     * 返回校验名称
     * @return 校验名称
     */
    @Override
    public String name() {
        return VALIDATOR_NAME;
    }
}
